﻿using ProjectFootball.MVC.Foundation.Repositories;
using ProjectFootball.MVC.Models.EntityFramework.Entities;
using System;
using System.Linq;

namespace ProjectFootball.MVC.Models.RepositoryProviders
{
    public static class MatchRepositoryExtensions
    {
        public static IQueryable<Match> GetMatchesByDateRange(this IDataRepository<Match> matchRespository, Guid competitionGuid, DateTime startDate, DateTime endDate)
        {
            return from m in matchRespository
                   where m.CompetitionGuid == competitionGuid
                   && m.Team1FT != null
                   && m.Team2FT != null
                   && m.IsApproved
                   && m.MatchDate >= startDate
                   && m.MatchDate <= endDate
                   select m;
        }

        public static Match SingleByPrimaryKey(this IDataRepository<Match> matchRepository, Guid id)
        {
            var matches = from m in matchRepository
                          where m.Pk == id
                          select m;

            return matches.Any() ? matches.Single() : null;
        }
    }
}